package org.gdpu.service.impl;

import org.gdpu.base.DaoSupport;
import org.gdpu.model.TestPaperInfo;
import org.gdpu.service.TestPaperInfoService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.Query;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class TestPaperInfoServiceImpl extends DaoSupport<TestPaperInfo> implements TestPaperInfoService {

    @Override
    public List<TestPaperInfo> findPaperInfoByUser(String account) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("owner", account);
        List<TestPaperInfo> testPaperInfos = (List<TestPaperInfo>) this.findByFields(TestPaperInfo.class, params);
        return testPaperInfos.size() > 0 ? testPaperInfos : null;
    }

    @Override
    public List<TestPaperInfo> findByTheme(String theme) {
        Query query = em.createQuery("from TestPaperInfo o where o.testPaperName like ?1");
        query.setParameter(1, "%" + theme + "%");
        return (List<TestPaperInfo>) query.getResultList();
    }

    @Override
    public List<TestPaperInfo> findTestPaperInfo(String theme) {
        List<TestPaperInfo> testPaperInfos = new ArrayList<TestPaperInfo>();
        List<TestPaperInfo> temp = this.findPaperInfoByUser(theme);
        if(temp!=null)
            testPaperInfos.addAll(temp);
        temp=this.findByTheme(theme);
        if(temp!=null)
        testPaperInfos.addAll(temp);
        return testPaperInfos.size() > 0 ? testPaperInfos : null;
    }
}
